package net.heri.xmppchat.activities.chatroom;

import java.util.LinkedList;

import net.heri.xmppchat.R;
import net.heri.xmppchat.persistence.MessageData;
import android.annotation.SuppressLint;
import android.content.Context;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.LinearLayout.LayoutParams;
import android.widget.TextView;

import com.google.inject.Inject;

public class ChatRoomAdapter extends BaseAdapter {

	private Context 					mContext;
	private LinkedList<MessageData> 	mMessages;
	
	@Inject
	public ChatRoomAdapter(Context context) {
		this.mContext = context;
		this.mMessages = new LinkedList<MessageData>();
	}
	
	@Override
	public int getCount() {
		return mMessages.size();
	}
	
	@Override
	public Object getItem(int position) {		
		return mMessages.get(position);
	}
	
	@SuppressLint("ResourceAsColor")
	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		
		MessageData message = (MessageData) this.getItem(position);
		
		ViewHolder holder; 
		if(convertView == null) {
			holder = new ViewHolder();
			convertView = LayoutInflater.from(mContext).inflate(R.layout.chat_room_message_row, parent, false);
			holder.message = (TextView) convertView.findViewById(R.id.message_text);
			convertView.setTag(holder);
		}
		else {
			holder = (ViewHolder) convertView.getTag();
		}
		
		holder.message.setText(message.getBody() );
		
		LayoutParams lp = (LayoutParams) holder.message.getLayoutParams();
		System.out.println();
		if(position % 2 == 0) {
			holder.message.setBackgroundResource(R.drawable.message_me_bg);
			lp.gravity = Gravity.RIGHT;
		}
		else {
			holder.message.setBackgroundResource(R.drawable.message_remote_bg);
			lp.gravity = Gravity.LEFT;
		}
		
		holder.message.setLayoutParams(lp);
		holder.message.setTextColor(mContext.getResources().getColor(R.color.textColor) );	
		
		/*
		if(message.isStatusMessage())
		{
			holder.message.setBackgroundDrawable(null);
			lp.gravity = Gravity.LEFT;
			holder.message.setTextColor(R.color.textFieldColor);
		}
		else
		{		
			//Check whether message is mine to show green background and align to right
			if(message.isMine())
			{
				holder.message.setBackgroundResource(R.drawable.message_me_bg);
				lp.gravity = Gravity.RIGHT;
			}
			//If not mine then it is from sender to show orange background and align to left
			else
			{
				holder.message.setBackgroundResource(R.drawable.message_remote_bg);
				lp.gravity = Gravity.LEFT;
			}
			holder.message.setLayoutParams(lp);
			holder.message.setTextColor(R.color.textColor);	
		}
		*/
		
		return convertView;
	}
	
	@Override
	public long getItemId(int position) {
		//Unimplemented, because we aren't using Sqlite.
		return 0;
	}
	
	public void addItem(MessageData record) {
		mMessages.add(record);
	}
	
	/**
	 * 
	 */
	private static class ViewHolder {
		TextView message;
	}
}
